package app

import (
	"os"
	"github.com/urfave/cli"
	"strings"
	"time"
	"hub.hddznet.com/app/framework/tools"
)

func Start(cmds []cli.Command) {
	if cfg == nil {
		panic("配置文件未读取")
	}
	//2. 实例化midway代理
	cmd := cli.NewApp()
	cmd.Name = MustValue("system", "name")
	cmd.Description = MustValue("system", "brief")
	cmd.Version = MustValue("system", "version")
	cmd.Action = func(c *cli.Context) error {
		return run()
	}
	cmd.Commands = []cli.Command{
		{
			Name:  "zip",
			Usage: "获取生产环境下的zip包",
			Action: func(c *cli.Context) error {
				//获取源文件列表
				dirstr := MustValue("system", "deploy.dir", "conf/")
				customer := MustValue("system", "customer", "")
				dirs := strings.Split(dirstr, ",")

				files := make([]*os.File, 0)

				for _, dir := range dirs {
					f, err := os.Open(dir)
					if err == nil {
						files = append(files, f)
					}
				}
				err := tools.Compress(files, customer+cmd.Version+"_"+time.Now().Format("0601021504")+".zip", cmd.Name)
				if err != nil {
					panic(err)
				}
				return nil
			},
		},
	}

	if len(cmds) > 0{
		cmd.Commands = append(cmd.Commands, cmds...)
	}

	if err := cmd.Run(os.Args); err != nil {
		panic(err.Error())
	}
}
